package com.irate.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.irate.activity.R;
import com.irate.cache.ImageFetcher;
import com.irate.cache.RecyclingImageView;
import com.irate.dao.UserDAO;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.model.User;
import com.irate.provider.ContentManager;

public class FriendListAdapter extends BaseAdapter {

	protected List<User> ldata;
	protected ImageFetcher imageFetcher;
	protected Context context;
	protected UserDAO userDAO;
	protected AsyncHttpResponseHandler handlerRemoveFriend;

	public FriendListAdapter(List<User> luser, ImageFetcher ifet, Context ct,
			AsyncHttpResponseHandler handler) {
		ldata = luser;
		imageFetcher = ifet;
		context = ct;
		userDAO = new UserDAO();
		handlerRemoveFriend = handler;
	}

	@Override
	public int getCount() {

		return ldata.size();
	}

	@Override
	public Object getItem(int arg0) {
		return ldata.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.friend_layout_item_friend,
					null);
		}

		RecyclingImageView friendAvatar = (RecyclingImageView) convertView
				.findViewById(R.id.friend_avatar);
		TextView friendName = (TextView) convertView
				.findViewById(R.id.friend_name);
		ImageView remove_friend = (ImageView) convertView
				.findViewById(R.id.friend_remove);

		final User u = (User) getItem(position);

		if (u.getAvatar() != null && !u.getAvatar().equals("") && !u.getAvatar().equals("null")) {
			imageFetcher.loadImage(u.getAvatar(), friendAvatar);
		} 
//		else {
//			imageFetcher
//					.loadImage(
//							"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQgDkbd_NkJZWeX8JnOsoVkotByhnQ2GWhMAaUvFolz6QaL29eysA",
//							friendAvatar);
//		}

		friendName.setText(u.getFirst_name() + " " + u.getLast_name());

		remove_friend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case DialogInterface.BUTTON_POSITIVE:
							ContentManager.getInstance().getListFriend()
									.remove(position);
							
							ldata.remove(position);
							notifyDataSetChanged();
							userDAO.removeFriend(u.getId(),
									handlerRemoveFriend, context, null);

							break;

						case DialogInterface.BUTTON_NEGATIVE:

							break;
						}
					}
				};

				AlertDialog.Builder builder = new AlertDialog.Builder(context);

				builder.setCancelable(false);
				builder.setMessage(
						"Are you sure to remove '" + u.getLast_name() + " "
								+ u.getFirst_name()
								+ "' from your friend list?")
						.setPositiveButton(
								context.getResources().getString(
										R.string.accep_request),
								dialogClickListener)
						.setTitle(
								context.getResources().getString(
										R.string.remove_friend))

						.setNegativeButton(
								context.getResources().getString(
										R.string.deny_request),
								dialogClickListener).show();
			}
		});

		return convertView;
	}

}
